package com.aaa.project.service.impl;


import com.aaa.entity.UComment;
import com.aaa.entity.UUserInfo;
import com.aaa.project.mapper.UCommentMapper;
import com.aaa.project.mapper.UUserInfoMapper;
import com.aaa.project.service.IUCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangxuhui
 * @since 2023-02-08
 */
@Service
public class UCommentServiceImpl extends ServiceImpl<UCommentMapper, UComment> implements IUCommentService {
    @Resource
    private UCommentMapper uCommentMapper;
    @Resource
    private UUserInfoMapper uUserInfoMapper;

    /**
     * 我的评论
     * @param principal
     * @param pageSize
     * @param pageCurrent
     * @param id
     * @return
     */
    @Override
    public PageInfo findMyComment(Principal principal, Integer pageSize, Integer pageCurrent, String id) {
        String name = principal.getName();
        // 根据当前的人的名字用户的id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("telephone",name);
        UUserInfo uUserInfo=uUserInfoMapper.selectOne(queryWrapper);
        //PageHelper.startPage(pageCurrent,pageSize);
        List<Map<String,Object>> list=uCommentMapper.findMyComment(uUserInfo.getId(),id);
        PageInfo pageInfo=new PageInfo<>(list);
        return  pageInfo;
    }
    /**
     * 查找所有评论
     * @param pageSize
     * @param pageCurrent
     * @param id
     * @return
     */
    @Override
    public PageInfo findAllComment(Integer pageCurrent, Integer pageSize,String id) {
//        PageHelper.startPage(pageCurrent, pageSize);
        List<UComment> list=uCommentMapper.findAllComment(id);
        System.out.println("list = " + list);
        for (int i = 0; i < list.size(); i ++) {
            String[] split = list.get(i).getImg().split(",");
            List<String> list1 = Arrays.asList(split);
            list.get(i).setImgs(list1);
        }
        PageInfo pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 关注店铺
     * @param bid
     * @param principal
     * @return
     */
    @Override
    public List<Map<String,Object>> findGuan(String bid,Principal principal) {
        System.out.println("bid = " + bid);
        String name=principal.getName();
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone",name);
        UUserInfo uUserInfo=uUserInfoMapper.selectOne(queryWrapper);
        String id = uUserInfo.getId();
        return uCommentMapper.findGuan(bid,id);
    }
}
